<?php
class SiteHelper extends Helper
{
	var $helpers = array('Session');
	var $mn=array(
1=>'Январь',
2=>'Февраль',
3=>'Март',
4=>'Апрель',
5=>'Май',
6=>'Июнь',
7=>'Июль',
8=>'Август',
9=>'Сентябрь',
10=>'Октябрь',
11=>'Ноябрь',
12=>'Декабрь',
);
	var $dn=array(
1=>'Пн',
2=>'Вт',
3=>'Ср',
4=>'Чт',
5=>'Пт',
6=>'Сб',
7=>'Вс',
);
	var $ratings=array(
	1=>'1',
	2=>'2',
	3=>'3',
	4=>'4',
	5=>'5',
	);
	var $lastseen=null;
	var $tz=null;

  function path($cats) {
  	$output="";
    $cc=count($cats); $i=1;
	foreach ($cats as $cdata) {
		$output.="<a class=\"catlink\" href=\"/group/".$cdata['name']."/\">".$cdata['description']."</a>";
		if ($i<$cc) $output.=" / ";
		$i++;
	}
    return $this->output($output);
  }

  function showCalendar($a,$y=null,$m=null) {
  	$output="";
    if(!$y && !$m) {
		$y=date("Y");
		$m=date("n");
	}

  $dayofmonth = date('t',mktime(0, 0, 0, $m, 2, $y));

  $day_count = 1;


  $num = 0;
  for($i = 0; $i < 7; $i++)
  {

	$dayofweek = date('w', mktime(0, 0, 0, $m, $day_count, $y));

    $dayofweek = $dayofweek - 1;
    if($dayofweek == -1) $dayofweek = 6;

    if($dayofweek == $i)
    {
      $week[$num][$i] = $day_count;
      $day_count++;
    }
    else
    {
      $week[$num][$i] = "";
    }
  }


  while(true)
  {
    $num++;
    for($i = 0; $i < 7; $i++)
    {
      $week[$num][$i] = $day_count;
      $day_count++;

      if($day_count > $dayofmonth) break;
    }

    if($day_count > $dayofmonth) break;
  }


  	$output.= '<table border="0" cellspacing="1" cellpadding="2" class="tlist tcal">';
	$output.= '<tr><td colspan="7" class="tmon">'.$this->mn[(int)$m]."</td></tr>";
	$dt='<tr class="thead">';
  foreach($this->dn as $value)
    $dt.="<td>".$value."</td>";
	$dt.="</tr>";
	$output.= $dt;

  for($i = 0; $i < count($week); $i++)
  {
    $output.= "<tr>";
    for($j = 0; $j < 7; $j++)
    {
      if(!empty($week[$i][$j]))
      {
        //if($j == 5 || $j == 6) $class=" class=tdcv"; else $class="";
        if(isset($a[$week[$i][$j]]))
					$output.= "<td><a href=\"/archive/".$y."/".sprintf("%02d",$m)."/".sprintf("%02d",$week[$i][$j])."/\">".$week[$i][$j]."</a></td>";
        else
					$output.= "<td>".$week[$i][$j]."</td>";
      }
      else $output.= "<td>&nbsp;</td>";
    }
    $output.= "</tr>";
  }
  $output.= "</table>";
  return $this->output($output);
}

	function pages($pages,$page,$url=null,$pname=null) {
		$var=""; $left=5; $right=4;
		if (!$pname) $pname="p";
		if (empty($pages) || $pages==1) return $this->output("");

  $toend=$pages-$page;
	if($page>5)
		$i=$page-5;
	else {
		$i=1;
		if($pages>10) $right=10-$page;
	}
  if ($toend < 4 ) {
			$right=$toend;
			$i=$page-(10-$toend)+1;
	}
	if($pages<=10) {
		$right=$pages-$page;
		$i=1;
	}

  if($url) {
		foreach($url as $k=>$v) {
			$var.="&".$k."=".urlencode($v);
		}
	}
	$output='<p class="pages">Pages: ';
	if ($page>1) $output.='<a class="pagelink" href="?'.$pname.'='.($page-1).$var.'">previous</a>&nbsp;';
  for($i;$i<$page+$right+1;$i++) {
	if($i==$page)
		$output.= "<span>".$i."</span>&nbsp;";
	else
		$output.= '<a class="pagelink" href="?'.$pname.'='.$i.$var.'">'.$i.'</a>&nbsp;';
	}
	if ($page<$pages) $output.='<a class="pagelink" href="?'.$pname.'='.($page+1).$var.'">next</a>';
	$output.= "</p>";
	return $this->output($output);
	}

  function nums($w,$n) {
    $t=array(
		'голос'=>array(0=>'ов', 1=>'', 2=>'а', 3=>'а', 4=>'а', 5=>'ов', 6=>'ов', 7=>'ов', 8=>'ов', 9=>'ов', 11=>'ов'),
		'очк'=>array(0=>'ов', 1=>'о', 2=>'а', 3=>'а', 4=>'а', 5=>'ов', 6=>'ов', 7=>'ов', 8=>'ов', 9=>'ов', 11=>'ов'),
		'ответ'=>array(0=>'ов', 1=>'', 2=>'а', 3=>'а', 4=>'а', 5=>'ов', 6=>'ов', 7=>'ов', 8=>'ов', 9=>'ов', 11=>'ов'),
    'пользовател'=>array(0=>'ей',1=>'ь',2=>'я',3=>'я',4=>'я',5=>'ей',6=>'ей',7=>'ей',8=>'ей',9=>'ей',11=>'ей'),
    'тем'=>array(0=>'',1=>'а',2=>'ы',3=>'ы',4=>'ы',5=>'',6=>'',7=>'',8=>'',9=>'',11=>''),
		'комментари'=>array(0=>'ев',1=>'й',2=>'я',3=>'я',4=>'я',5=>'ев',6=>'ев',7=>'ев',8=>'ев',9=>'ев',11=>'ев'),
		);
		$l=strlen((string)$n);
		$rev=strrev((string)$n);

	if($l>1 && $rev[1]==1) return $this->output($n." ".$w.$t[$w]['11']);
	foreach ($t[$w] as $k=>$v)
		if ($rev[0]==$k) return $this->output($n." ".$w.$t[$w][$k]);
	}

	function tags($tags) {
   	$output="";
		$i=1; $c=count($tags);
		foreach($tags as $tag) {
			$output.="<a class=\"taglink\" href=\"/tag/{$tag['tag']}/\">".$tag['tag']."</a>";
			if($i<$c) $output.=", ";
			$i++;
		}
		return $this->output($output);
	}

	function fdate($d=null,$conv=true,$format="d.m.y H:i") {
  	$dt=array(
		'Sunday'=>'воскресенье',
		'Monday'=>'понедельник',
		'Tuesday'=>'вторник',
		'Wednesday'=>'среда',
		'Thursday'=>'четверг',
		'Friday'=>'пятница',
		'Saturday'=>'суббота',
		'Sun'=>'вс',
		'Mon'=>'пн',
		'Tue'=>'вт',
		'Wed'=>'ср',
		'Thu'=>'чт',
		'Fri'=>'пт',
		'Sat'=>'сб',
		'January'=>'января',
		'February'=>'февраля',
		'March'=>'марта',
		'April'=>'апреля',
		'May'=>'мая',
		'June'=>'июня',
		'July'=>'июля',
		'August'=>'августа',
		'September'=>'сентября',
		'October'=>'октября',
		'November'=>'ноября',
		'December'=>'декабря',
		);

    if(is_null($this->tz)) {
			if($this->Session->check('User'))
				$this->tz=$this->Session->read('User.tz');
			else
				$this->tz=0;
		}

		//$format="j M Y H:i";
		if(!$d) $given_timestamp=time();
		else $given_timestamp = strtotime($d);

		$given_timestamp = $given_timestamp + (3600 * $this->tz);

		if($conv) {
		    //$current_timestamp = time() + (3600 * $tz);
			$current_timestamp = time();
	    	//$given_timestamp = $gmepoch + (3600 * $tz);
		    $diff = floor ($current_timestamp / 86400) - floor($given_timestamp / 86400);
		    //if($diff < 7) $format="D H:i";
		    if($diff == 0) $format="H:i";
		    if($diff == 1) $format="вчера, H:i";
	    }

		return $this->output(strtr(date($format, $given_timestamp),$dt));
	}

	function excerpt($text,$type='comment') {
		mb_internal_encoding("UTF-8");
		$text=strip_tags($text);
		if($type=='comment') {
			$text=mb_substr($text,0,50);
			if(mb_strlen($text)==50) $text.="…";
		} else {
			if(mb_strlen($text)>400) {
			preg_match('/^((.+?[\.\?\!])\s){3,5}/umsi',$text,$res);
			if(isset($res[0]))
				$text=$res[0];
			else
				$text=mb_substr($text,0,400);
			}
		}
		return $this->output($text);
	}

	function avatar($l,$a,$s=0) {
		if($s==0) {
			$size='width="48" height="48"';
			$sm='';
		}
		else {
			$size='width="24" height="24"';
			$sm='s';
		}
		if($a==0) {
			$text='<img class="avsm" src="/img/1'.$sm.'.gif" '.$size.'>';
		} else {
			$text='<img class="avatar" src="/pic/'.$l.'.jpg" '.$size.'>';
		}
		return $this->output($text);
	}

	function slink($title,$field,$sortfield,$sortorder,$nextsortorder) {
		if($sortfield==$field) {
			if($sortorder=="asc")
				$class="sasc";
			else
				$class="sdesc";
		}
		else
			$class="";
		$link='<a class="'.$class.'" href="?sort='.$field.'&order='.$nextsortorder.'">'.$title.'</a>';
		return $this->output($link);
	}

	function newtopic($date) {
    if(!$this->lastseen && $this->Session->check('User'))
			$this->lastseen=$this->Session->read('User.lastseen');
		if($this->lastseen) {
		if(strtotime($date)-strtotime($this->lastseen)>0)
			return $this->output('<span class="new">+</span>');
		}
	}

	function rate($pid, $rating, $active=1) {
		$fr=str_replace(".",",",round($rating,2));
		$rating=round($rating);
		$text=<<<EOD
		<span id="rating">
EOD;
		for($i=1; $i<=5; $i++) {
      if($i<=$rating)
				$class="star";
			else
				$class="star zero";

			if($active) {
				$link="/votes/rate/".$pid."/".$i."/";
        $text.=<<<EOD
<a href="$link" id="r$i" class="$class" title="{$this->ratings[$i]}">&nbsp;</a>
EOD;
			}
			else {
				$text.=<<<EOD
<span class="$class" title="{$this->ratings[$i]}">&nbsp;</span>
EOD;
			}
		}
    $text.=<<<EOD
		<b id="rated" title="average">$fr</b>
		</span>
EOD;
		return $this->output($text);
	}

	function rating($p,$r) {
		if($p==0) {
			return "";
		}
		$r=$p/$r;
		$fr=str_replace(".",",",round($r,2));
		$rating=round($r);
		$stars="";
		for($i=1; $i<=5; $i++) {
      if($i<=$rating)
				$star="star";
			else
				$star="starz";
			$stars.=<<<EOD
<img src="/img/{$star}.gif" width="12" height="12">
EOD;
		}
    $text=<<<EOD
<span class="rating" title="$fr">
$stars
</span>
EOD;
		return $this->output($text);
	}

}
?>